package com.example.order.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;


import com.example.order.R;
import com.example.order.pojo.Food;

import java.util.List;

public class FoodAdapter extends BaseAdapter implements View.OnClickListener {
    private List<Food> mContentList;
    private LayoutInflater mInflater;
    private Callback callback;


    public FoodAdapter(List<Food> mContentList, Context context, Callback callback) {
        this.mContentList = mContentList;
        this.mInflater = LayoutInflater.from(context);
        this.callback=callback;
    }

    @Override
    public void onClick(View v) {
        callback.click(v);
    }

    public interface Callback{
        void click(View v);
    }


    @Override
    public int getCount() {
        return mContentList.size();
    }

    @Override
    public Object getItem(int position) {
        return mContentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"InflateParams", "ViewHolder", "SetTextI18n"})
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        view = mInflater.inflate(R.layout.food_adapter, null);
        TextView text1=view.findViewById(R.id.food_text1);
        TextView text2=view.findViewById(R.id.food_text2);
        TextView text3=view.findViewById(R.id.food_text3);
        text1.setText(String.valueOf(mContentList.get(position).getId()));
        text2.setText(mContentList.get(position).getName());
        text3.setText(String.valueOf(mContentList.get(position).getPrice())+"元");
        Button button_add=view.findViewById(R.id.food_btn1);
        Button button_reduce=view.findViewById(R.id.food_btn2);
        //不应该传position,应该传food.id
        button_add.setTag(mContentList.get(position).getId());
        button_reduce.setTag(mContentList.get(position).getId());
        button_add.setOnClickListener(this);
        button_reduce.setOnClickListener(this);

        return view;
    }
}
